<template>
	<view class="login_wrap">
		<login-pop v-if="popFlag" @sendInfo="sendInfo" @closePop="closePop"></login-pop>
		<view class="head_item">
			<view class="head_img" v-if="avatar == ''">logo</view>
			<image v-else class="head_img" :src="avatar" mode=""></image>
			<view class="name">{{ nickname == '' ? '青创小程序' : nickname }}</view>
		</view>
		<!-- <view class="save_btn" @click="getInfo()">微信授权登录</view> -->
		<button class="save_btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">微信授权登录</button>
		<view class="agreement_text">登录即表示您同意<text @click="toAgr(1)">《用户协议》</text>和<text @click="toAgr(2)">《隐私政策》</text></view>
	</view>
</template>

<script>
	import loginPop from '@/components/login_pop/login_pop.vue'
	import { login } from '@/api/login.js'
	export default {
		components: {
			loginPop
		},
		data() {
			return {
				avatar: '',
				nickname: '',
				popFlag: false,
				mobile: '',
				openid: '',
			};
		},
		onLoad() {
			uni.hideHomeButton()
			let that = this
			wx.cloud.callFunction({
				name: 'getOpenid',
			})
			.then(res => {
				console.log('openid',res)
				that.openid = res.result.openId
			})
			.catch(err => {
				console.log(err)
			})
		},
		methods: {
			getInfo() {
				this.popFlag = true
			},
			closePop(e) {
				console.log(e)
				this.popFlag = false
			},
			sendInfo(e) {
				this.popFlag = false
				this.avatar = JSON.parse(e).avatarUrl
				this.nickname = JSON.parse(e).nickname
			},
			toAgr(index) {
				uni.navigateTo({
					url: '/pages/agreement/agreement?index=' + index
				})
			},
			getPhoneNumber(e) {
				let cloudID = e.detail.cloudID //开放数据ID
				let openid = ''
				let that = this
				if (!cloudID) {
					uni.showToast({
						title: '用户未授权',
						icon: 'none'
					})
					return
				}
				// 调用云函数获取手机号
				wx.cloud.callFunction({
					name: 'getPhone',
					data: {
						cloudID: e.detail.cloudID,
					}
				})
				.then(res => {
					login({
						openid: that.openid,
						mobile: res.result.list[0].data.phoneNumber
					}).then(res => {
						console.log(res)
						if (res.code == 1) {
							uni.setStorageSync('userInfo', res.data.userinfo)
							uni.showToast({
								title: '登录成功！',
								icon: 'none'
							})
							setTimeout(() => {
								uni.switchTab({
									url: '/pages/index/index'
								})
							}, 600)
						}
					})
				})
				.catch(err => {
					console.log(err)
				})
			},
		
				
		}
	}
</script>

<style lang="scss">
	.login_wrap {
		padding-top: 260rpx;
		.head_item {
			text-align: center;

			.head_img {
				width: 220rpx;
				height: 220rpx;
				line-height: 220rpx;
				text-align: center;
				border-radius: 120rpx;
				background: #E7E7E7;
				margin: 0 auto;
			}

			.name {
				margin-top: 22rpx;
			}
		}

		.save_btn {
			display: flex;
			width: 600rpx;
			height: 88rpx;
			padding: 12rpx 16rpx;
			justify-content: center;
			align-items: center;
			color: #fff;
			font-size: 32rpx;
			font-family: "PingFang SC";
			margin: 444rpx auto 62rpx;
			border-radius: 200rpx;
			border: 1rpx solid #ffffffe6;
			background: #28C445;
		}

		.agreement_text {
			text-align: center;
			color: #5F5F5F;
			font-size: 24rpx;

			text {
				color: #1C41F1;
			}
		}
	}
</style>